package graduate.cslg.com.universityshopping.adapter;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;

import java.util.List;

import de.hdodenhof.circleimageview.CircleImageView;
import graduate.cslg.com.universityshopping.R;
import graduate.cslg.com.universityshopping.bbs.BBSContentActivity;
import graduate.cslg.com.universityshopping.entity.PostListBean;

public class BBSItemAdapter extends RecyclerView.Adapter<BBSItemAdapter.ViewHolder> {

    private Context mContext;
    public static final String BBS_ID = "bbs_id";
    private List<PostListBean> mPostListBeans;

    public class ViewHolder extends RecyclerView.ViewHolder {

        RelativeLayout mRelativeLayout;
        CircleImageView userHead;
        TextView userName;
        TextView bbsTitle;
        TextView bbsContent;
        TextView bbsType;
        TextView userHits;
        TextView userReply;
        TextView userPostTime;

        public ViewHolder(View itemView) {
            super(itemView);
            mRelativeLayout = itemView.findViewById(R.id.bbs_layout_item);
            userHead = itemView.findViewById(R.id.user_head_img);
            userName = itemView.findViewById(R.id.user_name);
            bbsTitle = itemView.findViewById(R.id.bbs_title);
            bbsContent = itemView.findViewById(R.id.bbs_content);
            bbsType = itemView.findViewById(R.id.bbs_type);
            userHits = itemView.findViewById(R.id.user_hits);
            userReply = itemView.findViewById(R.id.user_reply);
            userPostTime = itemView.findViewById(R.id.user_post_time);
        }
    }

    public BBSItemAdapter(List<PostListBean> list) {
        this.mPostListBeans = list;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if (mContext == null) {
            mContext = parent.getContext();
        }
        View view = LayoutInflater.from(mContext).inflate(R.layout.layout_bbs_item, parent, false);
        final ViewHolder viewHolder = new ViewHolder(view);
        viewHolder.mRelativeLayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int position = viewHolder.getAdapterPosition();
                PostListBean mBean = mPostListBeans.get(position);
                int id = mBean.getBbs_id();
                Intent intent = new Intent(mContext, BBSContentActivity.class);
                intent.putExtra(BBS_ID, id);
                intent.putExtra("user_name", mBean.getUser_name());
                intent.putExtra("post_time", mBean.getUser_post_time());
                intent.putExtra("bbs_title", mBean.getBbs_title());
                intent.putExtra("bbs_content", mBean.getBbs_content());
                intent.putExtra("bbs_type", mBean.getBbs_type());
                mContext.startActivity(intent);
            }
        });
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        PostListBean postListBean = mPostListBeans.get(position);
        holder.userName.setText(postListBean.getUser_name());
        holder.bbsTitle.setText(postListBean.getBbs_title());
        holder.bbsContent.setText(postListBean.getBbs_content());
        holder.bbsType.setText(postListBean.getBbs_type());
        holder.userHits.setText(String.valueOf(postListBean.getUser_hints()));
        holder.userReply.setText(String.valueOf(postListBean.getUser_reply()));
        holder.userPostTime.setText(postListBean.getUser_post_time());
    }

    @Override
    public int getItemCount() {
        return mPostListBeans.size();
    }
}
